package bs.app.Client;

import bs.app.Constant.CConfig;
import bs.app.Constant.KeyConstant;
import bs.app.Main.CCanvas;
import bs.app.Res.CRess;
import bs.app.android.Graphics;

public class ChildrenMenu {
	public int xChildren, yChildren, wChildren, hChildren;
	public int element;
	public int index = 0;
	public String[][] data;
	public int Cry, AmDuong;
	public int distanceLine;
	public int NumberDivided = 2;
	public boolean PressOnce = false;
	public int VChildren = 0;
	public int type = 0;
	public int[] Color = { 0xa1a6a1, 0xf1941c };
	public int[] border = { 0x090b7a, 0x201404 };

	public ChildrenMenu() {
		distanceLine = 23;
	}

	public void init(int xchildren, int ychildren, int wchildren,
			int hchildren, int element, String[][] data, int type) {
		this.xChildren = CCanvas.xTranslate + xchildren;
		this.yChildren = ychildren;
		this.wChildren = wchildren;
		this.hChildren = hchildren;
		this.element = element;
		this.data = data;
		this.type = type;
	}

	public void paintChildrenMenu(Graphics g) {
		// border Children Menu
		g.setClip1(xChildren, yChildren, wChildren + 2, hChildren + 1);
		for (int i = 0; i < data.length; i++) {
			// if(CCanvas.s_Instan.ispaint(g.getClipX(), g.getClipY(),
			// g.getClipWidth(), g.getClipHeight(), xChildren, yChildren,
			// xChildren+wChildren,yChildren+i*23+VChildren))
			{
				g.setColor(0x8affff);
				g.drawRect(xChildren, yChildren + i * 23 + VChildren,
						wChildren, 23);
				g.setColor(0x122025);
				g.fillRect(xChildren + 1, yChildren + 1 + i * 23,
						wChildren - 1, element * 23 - 1);
				CCanvas.mediumfont.drawString(
						g,
						data[i][0],
						xChildren
								+ 2
								+ (wChildren - CCanvas.mediumfont
										.stringWidth(data[i][0])) / 2,
						yChildren + 5 + i * 23 + VChildren, 0, 0xa1a6a1,
						0x090b7a, true);
			}
		}
		if (CRess.menuchosen.getWidth() == 69)
			g.drawImage(CRess.menuchosen1, xChildren, yChildren + index * 23
					+ VChildren, Graphics.TOP | Graphics.LEFT);
		else
			g.drawImage(CRess.menuchosen, xChildren, yChildren + index * 23
					+ VChildren, Graphics.TOP | Graphics.LEFT);

		CCanvas.mediumfont.drawString(g, data[index][0], xChildren + 2
				+ (wChildren - CCanvas.mediumfont.stringWidth(data[index][0]))
				/ 2, yChildren + 5 + index * 23 + VChildren, 0, Color[1],
				border[1], true);
		g.setColor(0x8affff);
		g.drawLine(xChildren, yChildren, xChildren + wChildren, yChildren);
		g.drawLine(xChildren, yChildren + element * 23, xChildren + wChildren,
				yChildren + element * 23);
		g.restoreClip();
	}

	public void ScrollItem() {
		if (Cry > 0) {
			VChildren -= (Cry / NumberDivided + Cry % NumberDivided) * AmDuong;
			Cry -= (Cry / NumberDivided + Cry % NumberDivided);
		} else {
			Cry = 0;
			PressOnce = false;
		}
	}

	static int iNewIndex = 0;
	static int iOldIndex = 0;

	// public static boolean isTouching = false;
	public int pointerItems(int x, int y) {

		// isTouching = true;
		if (index < 2) {
			iOldIndex = index;
		} else if (index >= data.length - 2) {
			if (index == data.length - 1) {
				iOldIndex = 4;
			} else if (index == data.length - 2) {
				iOldIndex = 3;
			}
		} else {
			iOldIndex = 2;
		}
		int x0 = 0;
		int x1 = CCanvas.s_iWcanvas;
		int y0 = CConfig.HIEGHT_COMMNAND;
		int y1 = CCanvas.s_iHcanvas - CConfig.HIEGHT_COMMNAND;
		int iRow = 0;
		int iCol = 0;
		// y = y - y0;
		if (y >= yChildren && y < yChildren + element * 23) {

			iNewIndex = (y - yChildren) / distanceLine;
			if (iNewIndex >= 0 && iNewIndex < 5) {
				if (iNewIndex == iOldIndex) {
					CCanvas.keyPressed[KeyConstant.KEY_FIRE] = true;
					return 1;
				}
				if (iNewIndex < 2) {
					if (iNewIndex < iOldIndex) {
						if (PressOnce == false) {
							if (index > 0) {
								index += (iNewIndex - iOldIndex);
								if (index > 1) {
									Cry += distanceLine * (2 - iNewIndex);
									AmDuong = -1;

								} else if (index > 0 && iNewIndex == 0) {
									Cry += distanceLine;
									AmDuong = -1;
								}
							} else {
								index += (iNewIndex - iOldIndex);
							}
							PressOnce = true;
						}
					} else {
						index += (iNewIndex - iOldIndex);
					}

				} else if (iNewIndex > 2) {
					if (iNewIndex > iOldIndex) {
						if (PressOnce == false) {
							if (index < data.length - 1) {
								index += (iNewIndex - iOldIndex);
								if (index < data.length - 2) {
									Cry += distanceLine * (iNewIndex - 2);
									AmDuong = 1;

								} else if ((index < data.length - 1)
										&& (iNewIndex == 4)) {
									Cry += distanceLine;
									AmDuong = 1;
								}
							} else {
								index += (iNewIndex - iOldIndex);
							}

							PressOnce = true;
						}

					} else {
						index += (iNewIndex - iOldIndex);
					}
				} else {
					index += (iNewIndex - iOldIndex);
				}
			}
		}
		return -1;
	}

	public void pointerPressed(int px, int py) {
		if (CCanvas.isPointerPressed) // User PointerPressed
		{
			if (Pointer.SelectSoftKey(px, py) == 0) { // left softkey
				CCanvas.keyPressed[KeyConstant.KEY_LEFT_SOFTKEY] = true;
			} else if (Pointer.SelectSoftKey(px, py) == 3) { // right softkey
				CCanvas.keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY] = true;
			} else {
				pointerItems(px, py);
			}
		}
		update();
	}

	public void UpdateKeyPress(boolean[] keyPressed, boolean[] keyHold,
			int currentAsccii[]) {
		// if (isTouching)
		// return;
		update();

		if (keyPressed[KeyConstant.KEY_UP]) {
			//System.out.println("phim len");
			if (PressOnce == false) {
				if (index > 0) {
					index--;
					if (index < data.length - 3 && index > 1) {
						Cry += distanceLine;
						AmDuong = -1;
					}
				}
				PressOnce = true;
			}
		} else if (keyPressed[KeyConstant.KEY_DOWN]) {
			//System.out.println("phim xuong");
			if (PressOnce == false) {
				if (index < data.length - 1) {
					index++;
					if (index > 2 && index < data.length - 2) {
						Cry += distanceLine;
						AmDuong = 1;
					}
				}
				PressOnce = true;
			}
		} else if (keyPressed[KeyConstant.KEY_LEFT]) {
			//System.out.println("phim trai");
			SubMenuItem.isPaintChildren = false;
		} else if (keyPressed[KeyConstant.KEY_FIRE]
				|| keyPressed[KeyConstant.KEY_LEFT_SOFTKEY]) {
			//System.out.println("phim left softkey");
			if (type == 1) // search
			{
				// xu ly o menu tim kiem
				CCanvas.s_Instan.submenu.resetColorindex();
				CCanvas.searchpage = new SearchPage();
				//System.out.println("data index la gi : " + data[index][1] );
				
				// dua vao link yeu cau cu the
				CCanvas.searchpage.init(data[index][1].trim());
				CCanvas.s_Instan.connect.numberpage = 1;
				CCanvas.CurrentScreen = CCanvas.IsSearchPage;
				if (!CCanvas.s_Instan.connect.Link.contains(data[index][0])) {
					CCanvas.s_Instan.connect.Link.addElement(data[index][0]);
				}
			} else if (type == 2) {
				CCanvas.s_Instan.submenu.resetColorindex();
				CCanvas.s_Instan.LoadingState.timeForLoading = 50;
				CCanvas.s_Instan.LoadingState.time = CCanvas.s_Instan.LoadingState.fixtime;
				CCanvas.CurrentScreen = CCanvas.isChangeLoadingState;
				CCanvas.s_Instan.connect.setURL(data[index][1].trim());

				// /////// Link Name
				CCanvas.s_Instan.connect.Link.removeAllElements();
				CCanvas.s_Instan.connect.Link.addElement(data[index][0]);
				// ////// URL
				String Home = (String) CCanvas.s_Instan.connect.URL
						.elementAt(0);
				CCanvas.s_Instan.connect.URL.removeAllElements();
				CCanvas.s_Instan.connect.URL.addElement(Home);
				CCanvas.s_Instan.connect.URL.addElement(data[index][1].trim());
				CCanvas.s_Instan.connect.numberpage = 1;

				CCanvas.s_Instan.connect.RequestServerGetData();
			} else if (type == 3) {
				if (data[index][1].equals("Gift")) {
					CCanvas.s_Instan.submenu.resetColorindex();
					CCanvas.searchpage = new SearchPage();
					CCanvas.searchpage.init("/?ctrl=ListGift",
							"Gửi đến SĐT");
					CCanvas.s_Instan.connect.numberpage = 1;
					CCanvas.CurrentScreen = CCanvas.IsSearchPage;
					if (!CCanvas.s_Instan.connect.Link.contains(data[index][0])) {
						CCanvas.s_Instan.connect.Link
								.addElement(data[index][0]);
					}
				} else if (data[index][1].equals("PushSMS")) {
					CCanvas.s_Instan.submenu.resetColorindex();
					CCanvas.searchpage = new SearchPage();
					String link = (String) CCanvas.s_Instan.connect.URL
							.lastElement();
					if (link.equals(""))
						link = "host";
					CCanvas.searchpage.init("/?ctrl=ListGift&link=" + link,
							"Gửi đến SĐT");
					CCanvas.s_Instan.connect.numberpage = 1;
					CCanvas.CurrentScreen = CCanvas.IsSearchPage;
					if (!CCanvas.s_Instan.connect.Link.contains(data[index][0])) {
						CCanvas.s_Instan.connect.Link
								.addElement(data[index][0]);
					}
				} else {
					CCanvas.s_Instan.submenu.resetColorindex();
					CCanvas.s_Instan.LoadingState.timeForLoading = 50;
					CCanvas.s_Instan.LoadingState.time = CCanvas.s_Instan.LoadingState.fixtime;
					CCanvas.CurrentScreen = CCanvas.isChangeLoadingState;
					String link = "/?ctrl=ListGift&command=" + data[index][1];
					CCanvas.s_Instan.connect.URL.addElement(link);
					CCanvas.s_Instan.connect.Link.addElement(data[index][1]);
					CCanvas.s_Instan.connect.setURL(link);
					CCanvas.s_Instan.connect.RequestServerGetData();
				}
			}
		} else if (keyPressed[KeyConstant.KEY_RIGHT_SOFTKEY]) {
			CCanvas.s_Instan.submenu.rightMenuCommand.action.perform();
		}
	}

	public void update() {
		ScrollItem();
	}
}
